package com.wsjj.yjh;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

//读取socket文本流
//需要在linux中打开端口  命令为：  nc -lk 7777
public class WordCountStreamUnboundedDemo {
    public static void main(String[] args) throws Exception {
//        1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
//        2.读取hadoop102中7777端口号的数据
        DataStreamSource<String> hadoop102 = env.socketTextStream("hadoop102", 7777);
//        3.处理数据
        SingleOutputStreamOperator<Tuple2<String, Long>> sum = hadoop102.flatMap(
                        (String value, Collector<Tuple2<String, Long>> out) -> {
                            String[] s = value.split(" ");
                            for (String s1 : s) {
                                out.collect(Tuple2.of(s1, 1L));
                            }
                        })
                .returns(Types.TUPLE(Types.STRING,Types.LONG))   //防止泛型擦除   因为上面用的是lambda表达式
                .keyBy(value -> value.f0)
                .sum(1);
//        4.输出数据
        sum.print();
//        5.执行
        env.execute();

    }
}
